package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsCustomerFile;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.pojo.WmsCustomerFileConsignee;
import com.jy.wms.service.WmsCustomerFileConsigneeService;

import javax.naming.ServiceUnavailableException;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import com.github.pagehelper.Page;

import java.util.List;
import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsCustomerFileConsigneeController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Thu Oct 12 11:26:43 CST 2017
 */
@RestController
@RequestMapping("/wmsCustomerFileConsignee")
@Api(value = "/wmsCustomerFileConsignee", description = "关于客户档案收货人详细信息管理操作")
public class  WmsCustomerFileConsigneeController extends BaseController<WmsCustomerFileConsigneeService,WmsCustomerFileConsignee> {

    @ApiOperation(value = "客户档案收货人详细信息管理 查询客户档案收货人详细信息列表",response = JSONObject.class, notes = "查询客户档案收货人详细信息列表")
    @PostMapping(value = "/list/{sskhid}")
    public @ResponseBody JSONObject list(@RequestBody WmsCustomerFileConsignee entity, @PathVariable("sskhid") String sskhid,HttpSession session){
        Page<WmsCustomerFileConsignee> list = new Page<WmsCustomerFileConsignee>();
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
        entity.setSskhid(sskhid);
        try {
            list= baseService.baseSelectPage(entity);
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "客户档案收货人详细信息管理 新增客户档案收货人详细信息",response = JSONObject.class, notes = "新增客户档案收货人详细信息")
    public @ResponseBody JSONObject save(HttpSession session,@RequestBody WmsCustomerFileConsignee entity){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            entity.setId(UUIDUtils.uuid());
            entity.setSfyx("1");
            entity.setSfmr("0");
            //传入值信息校验，如果不符合要求则给出错误信息
            baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增客户档案收货人成功","", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增客户档案收货人失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增客户档案收货人成功",e.getMessage(), JSONObject.fromObject(entity).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增客户档案收货人失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "客户档案收货人详细信息管理 修改客户档案收货人详细信息",response = JSONObject.class, notes = "修改客户档案收货人详细信息")
    public @ResponseBody JSONObject edit(@RequestBody WmsCustomerFileConsignee entity){
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改客户档案收货人成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改客户档案收货人失败",e.getMessage(),"");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改客户档案收货人成功","", "");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改客户档案收货人失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "客户档案收货人详细信息管理 查询唯一客户档案基本信息",response = JSONObject.class, notes = "查询唯一客户档案收货人详细信息，'id'客户档案ID")
    @GetMapping(value = "/findOne/{id}")
    public @ResponseBody
    JSONObject getWmsOwnerById(@PathVariable("id") String id) {
        WmsCustomerFileConsignee wmsCustomerFileConsignee = new WmsCustomerFileConsignee();
        try {
            wmsCustomerFileConsignee = baseService.baseSelectByPrimaryKey(id);
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  JSONObject.fromObject(wmsCustomerFileConsignee);
    }

    @ApiOperation(value = "客户档案收货人详细信息管理 作废客户档案",response = JSONObject.class, notes = "作废客户档案收货人详细信息，'id'客户档案ID")
    @PostMapping(value = "/cancel")
    public @ResponseBody
    JSONObject cancel(@RequestBody SetAndModifyModel setAndModifyModel) {
        try {
            List<String> ids = setAndModifyModel.getIds();
            WmsCustomerFileConsignee wmsCustomerFileConsignee = new WmsCustomerFileConsignee();
            for (String id : ids) {
                wmsCustomerFileConsignee.setId(id);
                wmsCustomerFileConsignee.setSfyx("0");
                baseService.baseUpdateByPrimaryKeySelective(wmsCustomerFileConsignee);
            }
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"作废客户档案收货人成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"作废客户档案收货人失败",e.getMessage(),"");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"作废客户档案收货人成功","", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"作废客户档案收货人失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "客户档案收货人详细信息管理 激活客户档案",response = JSONObject.class, notes = "激活客户档案收货人详细信息，'id'客户档案ID")
    @PostMapping(value = "/activation")
    public @ResponseBody
    JSONObject activation(@RequestBody SetAndModifyModel setAndModifyModel) {
        try {
            List<String> ids = setAndModifyModel.getIds();
            WmsCustomerFileConsignee wmsCustomerFileConsignee = new WmsCustomerFileConsignee();
            for (String id : ids) {
                wmsCustomerFileConsignee.setId(id);
                wmsCustomerFileConsignee.setSfyx("1");
                baseService.baseUpdateByPrimaryKeySelective(wmsCustomerFileConsignee);
            }
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"激活客户档案收货人成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"激活客户档案收货人失败",e.getMessage(),"");
        }  catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"激活客户档案收货人成功","", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"激活客户档案收货人失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "客户档案收货人详细信息管理 设为默认",response = JSONObject.class, notes = "设为默认，'id'客户档案ID")
    @GetMapping(value = "/acquiesceIn/{id}")
    public @ResponseBody
    JSONObject acquiesceIn(@PathVariable("id") String id) {
        try {
            WmsCustomerFileConsignee wmsCustomerFileConsignee = baseService.selectById(id);
            wmsCustomerFileConsignee.setSfmr("1");
            baseService.acquiesceIn(wmsCustomerFileConsignee);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"收货地址设为默认成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"收货地址设为默认失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"收货地址设为默认成功","", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"收货地址设为默认失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "客户档案收货人详细信息管理 取消默认",response = JSONObject.class, notes = "取消默认，'id'客户档案ID")
    @GetMapping(value = "/removeAcquiesceIn/{id}")
    public @ResponseBody
    JSONObject removeAcquiesceIn(@PathVariable("id") String id) {
        try {
            WmsCustomerFileConsignee wmsCustomerFileConsignee = new WmsCustomerFileConsignee();
            wmsCustomerFileConsignee.setId(id);
            wmsCustomerFileConsignee.setSfmr("0");
            baseService.baseUpdateByPrimaryKeySelective(wmsCustomerFileConsignee);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"收货地址取消默认成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"收货地址取消默认失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"收货地址取消默认成功","", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"收货地址取消默认失败",e.getMessage(),"");
        }
    }

}
